import { LeaveRequest } from './../../../domain/leave-request';
import { LeaveRequestStateService } from './../../../service/leave-request-state.service';
import { AuthService } from '../../../service/auth.service';
import { ManagerService } from './../../../service/manager.service';
import { User } from 'src/app/domain/user';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';


/*

@Author:赵明洋

@Date: 2018-12-12

@员工请假查询功能实现

@Last Modified time: 2018-12-16
*/

@Component({
  selector: 'app-leave-request-state',
  templateUrl: './leave-request-state.component.html',
  styleUrls: ['./leave-request-state.component.css']
})
export class LeaveRequestStateComponent implements OnInit {
  private user: User;
  sid = AuthService.getUserId();
  private suggestion = null;

  LeaveRequests: LeaveRequest[];


  constructor(public router: Router, private leaverequeststate: LeaveRequestStateService, private managerService: ManagerService) {

    this.user = {
      id: null,
      user_name: null,
      name: null,
      age: null,
      sex: null,
      department: null, //部门
      position: null, //职务
      annual_leave_length: null, //年假上限
      remain_annual_leave: null, //剩余年假
    };

  }

  create() {
    this.router.navigateByUrl('/staff/leaverequeststate/0');
  }

  update(leaverequest: LeaveRequest) {
    this.router.navigateByUrl('/staff/leaverequeststate/' + leaverequest.id)
  }

  delete(leaverequest: LeaveRequest) {
    if (leaverequest.type == 3) {//年假
      if (leaverequest.state != 0 && leaverequest.state != 1) {
        this.user.remain_annual_leave = Number(this.user.remain_annual_leave) + Number(leaverequest.duration);
        this.managerService.updateUserleave(this.user)
          .subscribe();
      }
    }
    this.leaverequeststate.deleteLeaveRequest(leaverequest)
      .subscribe(data => this.getLeaveRequests());



  }




  getLeaveRequests() {
    // this.LeaveRequests = this.leaverequeststate.getLeaveRequests();       //调用服务的方法获取请求信息
    this.leaverequeststate.getLeaveRequests()
      .subscribe(LeaveRequests => this.LeaveRequests = LeaveRequests);
  }

  selectType(type: number) {
    if (type == 0) {
      return "病假";
    }
    if (type == 1) {
      return "婚假";
    }
    if (type == 2) {
      return "事假";
    }
    if (type == 3) {
      return "年假";
    }
  }

  selectState(state: number) {
    if (state == 0) {
      return "未通过";
    }

    if (state == 1) {
      return "已通过";
    }
    if (state == 2) {
      return "等待部门经理审批";
    }

    if (state == 3) {
      return "等待副总经理审批";
    }
    if (state == 4) {
      return "等待总经理审批";
    }
  }


  ngOnInit() {
    this.getUser();
    //调用注入服务的函数
  }

  getUser() {
    var id = AuthService.getUserId();
    if (id == 0) {
      alert("请先登录");
    }
    else {
      this.managerService.getUser(id)
        .subscribe(user => {
          this.user = user[0];
          this.getLeaveRequests();
        });
    }

  }

  lookSuggestion(suggestion: string) {
    this.suggestion = suggestion;
  }

  backSuggestion() {
    this.suggestion = null;
  }

}


